package com.qf.electronic.mall.user.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.qf.constant.RabbitmqConstant;
import com.qf.constant.RedisConstant;
import com.qf.dto.ResultBean;
import com.qf.entity.TUser;
import com.qf.mapper.TUserMapper;
import com.qf.service.IUserService;
import com.qf.util.SpringSecurityUtil;
import com.qf.util.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 作者：张健
 * 项目名：electronic-mall
 * 时间：2020/3/10  15:21
 * 描述：
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private TUserMapper mapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public ResultBean register(String phone, String code, String password) {
        //验证验证码
        String redisKey = StringUtils.getRedisKey(RedisConstant.REGISTER_PHONE, phone);
        String redisCode= (String) redisTemplate.opsForValue().get(redisKey);
        if (code.equals(redisCode)) {//验证码正确
            TUser user = new TUser();
            user.setPhone(phone);
            List<TUser> userList = mapper.select(user);
            if (userList!=null&&userList.size()!=0){
                return ResultBean.error("该手机号已注册过");
            }
            user.setCreateTime(new Date());
            user.setPassword(SpringSecurityUtil.getEncodePassword(password));
            //插入到数据库中
            int rows = mapper.insert(user);
            if (rows > 0) {
                return ResultBean.success("注册成功");
            } else {
                return ResultBean.error("注册失败！");
            }

        }

        return ResultBean.error("验证码错误！");
    }

    @Override
    public ResultBean mailRegister(String mail, String password) {
        TUser user = new TUser();
        user.setEmail(mail);
        List<TUser> userList = mapper.select(user);
        if (userList!=null&&userList.size()!=0){
            return ResultBean.error("该邮箱已注册过");
        }
        user.setPassword(SpringSecurityUtil.getEncodePassword(password));
        user.setCreateTime(new Date());
        user.setFlag(0);
        int rows = mapper.insert(user);
        if (rows>0){
            String uuid = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set(StringUtils.getRedisKey(RedisConstant.REGISTER_EMAIL,uuid),mail,15, TimeUnit.MINUTES);
            String data=mail+":"+uuid;
            rabbitTemplate.convertAndSend(RabbitmqConstant.MAIL_TOPIC_EXCHANGE,"mail.send.message",data);
            return ResultBean.success("注册成功");
        }

        return ResultBean.error("注册失败");
    }

    @Override
    public ResultBean mailActiveAccount(String uuid) {
        String redisKey = StringUtils.getRedisKey(RedisConstant.REGISTER_EMAIL, uuid);
        String email  = (String) redisTemplate.opsForValue().get(redisKey);
        Example example=new Example(TUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("email",email);
        TUser user = new TUser();
        user.setFlag(1);
        int rows = mapper.updateByExampleSelective(user, example);
        if (rows>0){
            return ResultBean.success("激活成功");
        }
        return ResultBean.error("激活失败");
    }

    @Override
    public ResultBean login(String username, String password) {
        Example example = new Example(TUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (username.indexOf("@")!=-1){
            criteria.andEqualTo("email",username);
            criteria.andEqualTo("password",SpringSecurityUtil.getEncodePassword(password));
            TUser user = mapper.selectOneByExample(example);
            if (user==null){
                return ResultBean.error("用户名或密码错误");
            }else {
                if (user.getFlag()==0){
                    return ResultBean.error("用户账号未激活");
                }
            }
            return ResultBean.success(user);
        }
        criteria.andEqualTo("phone",username);
        criteria.andEqualTo("password",SpringSecurityUtil.getEncodePassword(password));
        TUser user = mapper.selectOneByExample(example);
        if (user==null){
            return ResultBean.error("用户名或密码错误");
        }
        return ResultBean.success(user);
    }

    @Override
    public ResultBean checkIsLogin(String uuid) {
        System.out.println(uuid);
        String redisKey = StringUtils.getRedisKey(RedisConstant.USER_LOGIN, uuid);
        Object o = redisTemplate.opsForValue().get(redisKey);
        if (o!=null){
            String s = JSON.toJSONString(o);
            TUser user = JSON.parseObject(s, TUser.class);
            user.setPassword("");
            return ResultBean.success(user,"用户已登录");
        }
        return ResultBean.error("用户未登录");
    }
}
